# 导入模块
import cv2

# 摄像头
cap = cv2.VideoCapture(0)
flag = 1
num = 1
while cap.isOpened():
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    k = cv2.waitKey(1) & 0xFF
    if k == ord('s'): # 按下s键，保存图片cv2.
        cv2.imwrite('./images/faces/name/{}.jpg'.format(num), frame)
        print('{}.jpg saved'.format(num))
        num += 1
    elif k == ord(' '): # 按下空格键退出
        break
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 关闭窗口